const { ccclass, property } = cc._decorator;

@ccclass
export default class lightText extends cc.Component {
    @property({ type: Number, displayName: '播放速度' })
    playSpeed: number = 1;
    @property({ type: Number, displayName: '高亮宽度缩放' })
    lightWidthScale: number = 1;
    @property({ type: Boolean, displayName: '文字是否居中' })
    isMiddle: boolean = true;

    @property({ type: cc.Color, displayName: '颜色' })
    color: cc.Color = null;

    @property(cc.Material)
    public text_material: cc.Material = null;

    private time: number = -this.lightWidthScale * 0.5;
    private _material: cc.MaterialVariant;
    private is_start = false;
    private line_count: number = 3;
    private line_idx: number = 0;

    private text_len = [24, 24, 8];

    update(dt) {
        if (this.is_start) {
            this.time += dt * this.playSpeed;
            if (this._material) {
                this._material.setProperty('time', this.time);
            }
        }
    }

    init() {
        this._material = this.node.getComponent(cc.Label).getMaterial(0);
        this._material.setProperty('is_stop', 0);
        this._material.setProperty('line_count', this.line_count);
        this._material.setProperty('line_idx', this.line_idx);
        this._material.setProperty('light_width_scale', this.lightWidthScale);
        this._material.setProperty('color_sr', this.node.color.getR());
        this._material.setProperty('color_sg', this.node.color.getG());
        this._material.setProperty('color_sb', this.node.color.getB());
        this._material.setProperty('color_r', this.color.getR());
        this._material.setProperty('color_g', this.color.getG());
        this._material.setProperty('color_b', this.color.getB());

        if (this.isMiddle) {
            this.playNext(this.text_len[this.line_idx]);
        } else {
            this.schedule(() => {
                this.line_idx++;
                if (this.line_idx > this.line_count) {
                    this.stop();
                    return;
                }
                this._material.setProperty('line_idx', this.line_idx);
            }, 10 / this.playSpeed);
        }
    }

    playNext(text_len) {
        this.scheduleOnce(() => {
            this.line_idx++;
            if (this.line_idx >= this.line_count) {
                this.stop();
                console.log('结束');
                return;
            }
            this._material.setProperty('line_idx', this.line_idx);
            this.setStartTime(this.text_len[this.line_idx]);
            this.playNext(this.text_len[this.line_idx]);
        }, (10 * text_len) / 24 / this.playSpeed);
    }

    setSpeed(text_len) {
        this.playSpeed *= text_len / 24;
    }

    setStartTime(text_len) {
        this.time += ((24 - text_len) / 2 / 24) * 10;
    }

    play() {
        this.unscheduleAllCallbacks();
        this.line_count = this.text_len.length;
        this.line_idx = 0;
        this.time = -this.lightWidthScale * 0.5;
        this.node.getComponent(cc.Label).setMaterial(0, this.text_material);
        this.init();
        this.is_start = true;
    }

    pause() {
        this.is_start = false;
    }

    continue() {
        this.is_start = true;
    }

    stop() {
        this.unscheduleAllCallbacks();
        this.line_idx = 0;
        this.time = -this.lightWidthScale * 0.5;
        this.is_start = false;
        this._material.setProperty('is_stop', 1);
    }
}
